package com.lyc.common.interceptor;

import java.util.Objects;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.lyc.common.constants.CommonConstants;
import com.lyc.common.constants.DepartCommonConstant;
import com.lyc.common.holder.UserContextHolder;
import com.lyc.common.util.RequestUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;

/**
 * Feign拦截器
 * @Author 李大神仙
 * @Date 2024/2/26 20:47
 * @Version 1.0
 */
@Slf4j
@Component
public class FeignRequestInterceptor implements RequestInterceptor {
    @Value("${spring.application.name}")
    private String appName;

    //@Resource
    //private DepartProperties departProperties;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        String feignTargetName = requestTemplate.feignTarget().name();
        //先从请求头中获取认证信息
        String authorization = RequestUtil.getAuthorization();
        String appCode = RequestUtil.getHeader(RequestUtil.ENCRYPT);
        //为空再从当前用户上下文中获取
        UserContextHolder userContext = UserContextHolder.getInstance();
      /*  if ((StringUtils.isBlank(authorization) || StringUtils.isBlank(appCode)) && Objects.nonNull(userContext.getContext())){
            //authorization = userContext.getAuthorizotion();
            //appCode = userContext.getAppCode();
        }*/
        //是内部服务
        if (DepartCommonConstant.NoAuthServiceName.DEPART_BUSINESS.equalsIgnoreCase(feignTargetName)
                && DepartCommonConstant.NoAuthServiceName.DEPART_SYSTEM.equalsIgnoreCase(feignTargetName)){
            requestTemplate.header(CommonConstants.INNER_SERVICE,CommonConstants.TRUE_STR);
            requestTemplate.header(DepartCommonConstant.DLHeaderConstant.ENCRYPTION_HEADER,appCode);
        }
        requestTemplate.header(RequestUtil.AUTHORIZATION,authorization);
        //requestTemplate.header(RequestUtil.ENCRYPT,departProperties.getDefaultAppCode());
    }
}
























